Get-ChildItem -File -Recurse -LiteralPath "C:\test [1]" does not return any information

I'm running Powershell v3.0 on a Windows 7 machine

I have a script to return recursive file counts for all directories for a give path:

$myPath = Get-ChildItem -Directory

foreach ($subDirectory in $myPath) {

    Write-Output Get-ChildItem -File -Recurse -LiteralPath $subDirectory.FullName | Measure-Object -Property length | Select-Object -ExpandProperty Count

}

This script works great until I have directories with square brackets ("[" or "]") in the directory name.  In that case, nothing is returned.  In fact, running the following script for a real folder with files in it displays nothing on the console when there are definitely files in this directory:

    Get-ChildItem -File -Recurse -LiteralPath ".\test [1]"

Any ideas?  Is this a known bug?  I've searched this forum and others and have found nobody with the same issue, so I suspect I'm missing something.

Thanks in advance,

Ryan

May 6th, 2013 4:00pm

Does this help as a start ? Quoted from http://technet.microsoft.com/en-us/library/ff730956.aspx

$myPath = Get-ChildItem -Directory 'c:\temp\*``[*'
foreach ($subDirectory in $myPath) {
    Write-Output $subDirectory.FullName
    Write-Output Get-ChildItem -File -Recurse -LiteralPath $subDirectory.FullName | Measure-Object -Property length | Select-Object -ExpandProperty Count
}

  • Proposed as answer by ImMax Monday, May 06, 2013 4:25 PM
Free Windows Admin Tool Kit Click here and download it now
May 6th, 2013 4:19pm

The quoted query would return only directories containing square brackets, though I wanted my query to return all results (square brackets or not).  However I may be able to use this to concatenate results with a query for folders not containing square brackets.  I would rather not do the concatenation of results for the following reasons:

1.  There are at least 4 separate queries I would have to write: no brackets, only the opening bracket "[", only the closing bracket "]", and containing both brackets.

2.  I hate work-arounds, but I'll use them if the "correct" way does not work

Thanks for the idea, I may use it if there is indeed no single-line way of doi

May 6th, 2013 4:47pm

how about this? or am i missing something

Get-ChildItem -Recurse -Path ".\test*

Free Windows Admin Tool Kit Click here and download it now
May 6th, 2013 5:10pm

"Test [1]" was just an example, I actually won't know the names of the folders in advance.  Also, the issue is retrieving the individual (recursive) file counts for each directory in the current directory.
May 6th, 2013 7:36pm

Ended up escaping all brackets and using the normal -Path attribute:


$myPath = Get-ChildItem -Directory

foreach ($subDirectory in $myPath) {

    Write-Output Get-ChildItem -File -Recurse -Path $subDirectory.FullName.Replace("[","``[").Replace("]","``]") | Measure-Object -Property length | Select-Object -ExpandProperty Count

}

I think there's a bug using the -LiteralPath attribute; after reading the following technet article, I realized how few characters would need escaped for the solution and just decided on good ol' .Replace:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa717088%28v=vs.85%29.

Free Windows Admin Tool Kit Click here and download it now
May 6th, 2013 7:54pm

Did you ever report this as a bug?
October 5th, 2013 8:39pm

I just reported this. Vote here if you think this should be fixed: https://connect.microsoft.com/PowerShell/feedbackdetail/view/1707779/

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 11:19am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics